Skip to content

Conversation

@mldwg
Copy link
Contributor

@mldwg mldwg commented Feb 23, 2022

The accessor functions for inheritance relations are overwritten in PolymorphicModel. This also disables the caching usually provided by django, creating multiple redundant SQL queries in certain situations:

parent = Parent.object.get(id=1)
c1 = parent.child  # retrieves child from db
c2 = parent.child  # retrieves child from db again!

This contradicts the default django behavior and can cause unnecessary overhead.

This PR fixes that issue by using the caching mechanism of the underlying field.
The new unit test test_polymorphic__accessor_caching validates this new feature.

@mldwg mldwg changed the title Caching in ingeritance accessor functions Caching in inheritance accessor functions Feb 23, 2022
@bckohan bckohan requested a review from Copilot December 7, 2025 06:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds caching to inheritance accessor functions in PolymorphicModel to align with Django's default behavior and avoid redundant database queries. Previously, the overridden accessor functions would execute a new database query on each access, which contradicts Django's standard caching behavior for related objects.

Key changes:

  • Modified accessor functions to use Django's field caching via get_cached_value() and set_cached_value()
  • Added comprehensive test coverage for both forward and reverse accessors
  • Ensured cache is properly cleared by refresh_from_db()

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/polymorphic/models.py Updated create_accessor_function_for_model to implement caching using Django's field caching mechanism, extracting the field object from descriptors to access cache methods
src/polymorphic/tests/test_orm.py Added test_polymorphic__accessor_caching to verify that inheritance accessors return cached objects on repeated access and that cache is cleared after refresh_from_db()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Dec 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.53%. Comparing base (11d2b74) to head (4993752).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #510   +/-   ##
=======================================
  Coverage   75.53%   75.53%           
=======================================
  Files          21       21           
  Lines        1345     1345           
  Branches      213      213           
=======================================
  Hits         1016     1016           
  Misses        257      257           
  Partials       72       72           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Co-authored-by: otto001 <matteo.ludwig@gmail.com>
Co-authored-by: Brian Kohan <bckohan@gmail.com>
Copy link
Contributor

@bckohan bckohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, sorry it took so long to merge - this project was leaderless for a number of years. Great test!

@bckohan bckohan merged commit f34061a into jazzband:master Dec 7, 2025
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants